home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / C Reference Card 2.3 / C Reference Card / C Reference Card.rsrc / TEXT_410_10.txt < prev    next >
Encoding:
Text File  |  1995-11-14  |  3.1 KB  |  141 lines

  1. EXCEPTIONS : basics, sample usage
  2. _________________________________________________________________________
  3.  
  4.  
  5. BASICS OF EXCEPTION HANDLING
  6.  
  7. One of the newer features of C++ is exception (or error) handling.  You use the 'try', 'catch', and 'throw' keywords for this purpose.  When programming, you tell the compiler to 'try' a section of code.  If an error occurs in the section (or in any functions in the section), you 'throw' the exception.  The exception is then handled like a hot potato and is passed up the ladder until it is caught.  If an exception is not caught, the program terminates.  Here's the basic syntax*:
  8.  
  9.   try
  10.   {
  11.     f1();
  12.     f2();
  13.     f3();
  14.   }
  15.   catch(const char *s)   // catch string error
  16.   {
  17.     // ...
  18.   }
  19.   catch(...)             // catch all other exceptions
  20.   {
  21.     cout << "Caution!";  // partially respond
  22.     throw;               // pass the exception
  23.   }
  24.  
  25.  
  26.   void f2(void)
  27.   {
  28.     // ...
  29.  
  30.     throw "Input Error!";
  31.   }
  32.  
  33. You may wish to 'throw' a class object rather than a string or error code.  An example of this can be shown as follows:
  34.  
  35.   class MyClass
  36.   {
  37.       int *values;
  38.       int maxSize;
  39.     public:
  40.       class HotPotatoe { };  // exception class
  41.       void aMethod( void );
  42.   } AnObject;
  43.  
  44.  
  45.   void MyClass::aMethod( void )
  46.   {
  47.     // ...
  48.  
  49.     if( error )
  50.       throw HotPotatoe();
  51.   }
  52.  
  53.  
  54.   void f( void )
  55.   {
  56.     try
  57.     {
  58.       AnObject.aMethod();
  59.     }
  60.     catch( MyClass::HotPotatoe )
  61.     {
  62.       // handle error
  63.     }
  64.   }
  65.  
  66.  
  67.  
  68. SAMPLE USAGE OF EXCEPTION HANDLING
  69.  
  70. The following code snippet provides an example of a compiler-specific implementation of exception handling.  Future versions of C++ compilers will likely incorporate the standard exception handling syntax described above.
  71.  
  72.   // exceptions.cp
  73.  
  74.   /*
  75.      Symantec THINK C 7.0 specific code.
  76.   */
  77.  
  78.   #include <iostream.h>
  79.   #include <Exceptions.h>
  80.   
  81.   class HotPotatoe TCL_EXCEPTION_CLASS
  82.   {  
  83.     public:
  84.       TCL_DECLARE_CLASS
  85.       HotPotatoe(double A, double B) {a=A; b=B;}
  86.       double  a,b;
  87.   };
  88.   TCL_DEFINE_CLASS_M0(HotPotatoe);
  89.  
  90.  
  91.   static void f(double x)
  92.   {
  93.     double max = 3.14159;
  94.  
  95.     if( x > max )
  96.       throw_( HotPotatoe(max,x) );
  97.   }
  98.  
  99.  
  100.   void main( void )
  101.   {
  102.     TCL_BREAK_ON_CATCH(false);
  103.   
  104.     try_
  105.     {  
  106.       cout << "Example One:" << endl;
  107.       f(5.0);
  108.     }
  109.     catch_reference_( HotPotatoe, oo )
  110.     {
  111.       cout << "limit: " << oo.a << "   value: " << oo.b 
  112.       << endl << endl;
  113.     }
  114.     end_try_
  115.  
  116.     try_
  117.     {
  118.       cout << "Example Two:" << endl;
  119.       try_    
  120.       {
  121.         f(5.0);
  122.       }
  123.       catch_all_()
  124.       {
  125.         cout << "exception caught..." << endl;
  126.  
  127.         cout << "rethrow exception..." << endl;
  128.         throw_same_();
  129.       }
  130.       end_try_
  131.     }
  132.     catch_all_()
  133.     {
  134.       cout << "rethrown exception caught." << endl;
  135.     }
  136.     end_try_
  137.   }
  138.   // end exceptions.cp
  139.  
  140. _________________________________________________________________________
  141. * Exception handling is new enough that my current compiler doesn't support the 'try', 'catch', and 'throw' keywords (at least not directly), so I don't have any *real* example code segments verified and compiled.